package gui;

import framework.Color;
import framework.GraphicsWrapper;
import framework.Rectangle;

public class Label implements Component {
	
	private String my_text;
	private int my_height;
	private int my_stringHeight;
	private int my_stringWidth;
	private int my_width;
	private boolean my_dirty = true;
	private GraphicsWrapper my_graphics;
	private int my_padding = 0;
	private Color my_foreground = Color.WHITE;
	private Color my_background = new Color(20, 20, 20, 200);
	
	public Label(final String the_text) {
		my_text = the_text;
	}
	

	@Override
	public Rectangle getMinSize(final GraphicsWrapper the_graphics) {
		final Rectangle bounds = the_graphics.getStringBounds(my_text);
		final int width = (int) bounds.getWidth();
		final int height = (int) bounds.getHeight();
		my_stringHeight = height;
		my_stringWidth = width;
		my_padding = my_stringHeight * 16 / 12;
		return new Rectangle(width + 2 * my_padding, height + 2 * my_padding);
	}

	@Override
	public Rectangle getSize() {
		return new Rectangle(my_width, my_height);
	}

	@Override
	public void setSize(int the_width, int the_height) {
		my_width = the_width;
		my_height = the_height;
	}
	
	@Override
	public void setSize(final Rectangle the_size) {
		my_width = (int) the_size.getWidth();
		my_height = (int) the_size.getHeight();
	}

	
	public void validate(final GraphicsWrapper g) {
		Rectangle size = getMinSize(g);
		my_width = (int)size.getWidth();
		my_height = (int)size.getHeight();
		my_dirty = false;
	}
	@Override
	public void render(GraphicsWrapper g) {
		if (my_dirty) {
			validate(g);
		}
		g.saveMatrix();
		g.setColor(my_background);
		g.fillRect(0, 0, my_width, my_height);
		g.setColor(my_foreground);
		g.drawString(my_text, (my_width - my_stringWidth) / 2, (my_height - my_stringHeight) / 2 + my_stringHeight - g.getMaxDescent());
		g.restoreMatrix();
	}
	
	public void addTouchListener(TouchListener the_listener) {
		
	}

}
